//
//  myWkWebViewController.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/19.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "myWkWebViewController.h"

#import <WebKit/WebKit.h>



@interface myWkWebViewController ()<WKNavigationDelegate>

{
    NSDictionary *payInfo;  // 支付信息
    NSString *shareUrl;
    UIActivityIndicatorView *activityIndicator;
}
@property (strong,nonatomic) WKWebView *mywebview;
@property (nonatomic, strong) UIProgressView *progressView;//设置加载进度条


@end

@implementation myWkWebViewController
- (void)dealloc{
    [self.mywebview removeObserver:self forKeyPath:@"estimatedProgress"];
      if ([self.mywebview isLoading]) {
        [self.mywebview stopLoading];
        self.mywebview.navigationDelegate = nil;
        self.mywebview.UIDelegate = nil;
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup aft
    
    _mywebview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-64)];
    _mywebview.backgroundColor = [UIColor clearColor];
    NSString *urlString = [_myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    
    self.mywebview.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
    [self.mywebview loadRequest:[NSURLRequest requestWithURL:url]];
    self.mywebview.navigationDelegate = self;
    [self.view addSubview:_mywebview];
    _mywebview.scrollView.alwaysBounceHorizontal = NO;
    _mywebview.scrollView.alwaysBounceVertical = NO;
    
    //添加进度条的KVO
    [self.mywebview addObserver:self
                     forKeyPath:@"estimatedProgress"
                        options:NSKeyValueObservingOptionNew
                        context:nil];
    
    [self toreduce];
    
    UILabel *lav = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 24)];
    lav.textColor = [UIColor blackColor];
    lav.backgroundColor = [UIColor clearColor];
    lav.font = [UIFont systemFontOfSize:18];
    lav.text = _toTitle;
    lav.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = lav;
}


//减少角标数字
- (void)toreduce{
    NSDictionary *dic = [userDefaults objectForKey:@"NocationList"];
    NSMutableArray *arr = dic.mutableCopy;
    
    NSMutableDictionary *newdic = ((NSDictionary *)arr[2]).mutableCopy;
    if([newdic[@"newItem"] intValue]>0){
        NSString *number = [NSString stringWithFormat:@"%d",[newdic[@"newItem"] intValue]-1];
        [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Notice/unreadChange" isactive:YES parameter:@{@"number":number,@"flag":@"notice"} successBlock:^(PPHttpRequest *request, id responseObj) {
             newdic[@"newItem"] = [NSString stringWithFormat:@"%d",[responseObj[@"notice"] intValue]];
            arr[2] = newdic;
            [userDefaults setObject:arr forKey:@"NocationList"];
        } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
            [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        }];
    }
}

-(UIProgressView *)progressView{
    if (!_progressView) {
        _progressView = [[UIProgressView alloc]
                         initWithProgressViewStyle:UIProgressViewStyleDefault];
        _progressView.frame             = CGRectMake(0, 0, SCREEN_W, 5);
        
        [_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255
                                                         green:240.0/255
                                                          blue:240.0/255
                                                         alpha:1.0]];
        _progressView.progressTintColor = [UIColor redColor];
        
        [self.view addSubview:_progressView];
    }
    return _progressView;
}

#pragma mark -KVO的方法
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSString *,id> *)change
                       context:(void *)context
{
    if ([keyPath isEqualToString:@"estimatedProgress"])
    {
        [self.progressView setAlpha:1.0f];
        BOOL animated = self.mywebview.estimatedProgress > self.progressView.progress;
        [self.progressView setProgress:self.mywebview.estimatedProgress
                              animated:animated];
        
        if (self.mywebview.estimatedProgress >= 1.0f) {
            [UIView animateWithDuration:0.3f
                                  delay:0.3f
                                options:UIViewAnimationOptionCurveEaseOut
                             animations:^{
                                 [self.progressView setAlpha:0.0f];
                             }
                             completion:^(BOOL finished) {
                                 [self.progressView setProgress:0.0f animated:NO];
                             }];
        }
    }else{
        [super observeValueForKeyPath:keyPath
                             ofObject:object
                               change:change
                              context:context];
    }
}


#pragma mark --WKWebView
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    decisionHandler(WKNavigationActionPolicyAllow);
}

-(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
    NSLog(@"createWebViewWithConfiguration");
    if (!navigationAction.targetFrame.isMainFrame) {
        [webView loadRequest:navigationAction.request];
    }
    return nil;
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    WKNavigationResponsePolicy  policy = WKNavigationResponsePolicyAllow;
    decisionHandler(policy);
}

@end
